using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Utilities.Menu.UIElements;
using Services;
using WAS.Logic.SampleLevel;
using WAS;
using System.IO.IsolatedStorage;
using System.IO;
using WAS.Resources;
using Utilities;
using System.Threading;

namespace WAS.Menu
{
    public class MainMenu : DrawableGameComponent
    {

        enum MenuState
        {
            MainMenu,
            Credits,
            Controls,
            HighScores,
            ModeSelection
        }

        SpriteBatch sprite_batch;
        InputManager input_manager;

        Matrix screen_transform;
        Box box;
        Texture2D tutorial1, tutorial2, tutorial3, tutorial4, tutorial5, tutorial6, tutorial7, tutorial8, tutorial9, confirm, not_conferm;
        Texture2D audio_on,audio_off, audio_bar;

        int tutorial_page = 0;

        Rectangle top_left, middle_left, mini_top_left, mini_middle_left, mini_bottom_left, mini_top_middle, mini_bottom_middle, confirm_not_confirm_rectangle;
        Rectangle audio_rect_bar;

        float alpha = 1.0f, wait_time_to_tap = 0.5f;
        bool increaseAlpha = true, campaign_completed = false;
        MenuState menu_state = MenuState.MainMenu, next_state;

        bool resume_option = false;
        bool show_tutorial;
        bool game_tutorial = false;
        bool audiostate;

        public MainMenu(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            input_manager = new InputManager(game);
            game.Components.Add(input_manager);

            var wakastats = Leaderboard.GetWakaModeStats();
            show_tutorial = wakastats.showAlwaysTutorial;
            resume_option = wakastats.LevelScore > 0;

            if (MediaPlayer.State == MediaState.Playing)
            {
                Leaderboard.SaveWakaModeStats(new WakaShipModeEntry(
                                                        wakastats.showAlwaysTutorial,
                                                        false,
                                                        wakastats.AsteroidHits,
                                                        wakastats.TimeToAddOrRemove,
                                                        wakastats.TimeToEndGame,
                                                        wakastats.ShipsDestroyed,
                                                        wakastats.BossDestroyed,
                                                        wakastats.LevelScore,
                                                        wakastats.Level
                                                        ));
                audiostate = false;
            }
            else
            {
                audiostate = wakastats.AudioState;
            }



            this.UpdateOrder = (int)ComponentOrders.TRANSITION + 1;
            this.DrawOrder = (int)ComponentOrders.UI;
        }

        public override void Initialize()
        {
            sprite_batch = Services.GetService(typeof(SpriteBatch)) as SpriteBatch;

            var input_service = Services.GetService(typeof(IInputManager)) as IInputManager;
            input_service.OnTap += on_tap;

            base.Initialize();
        }

        Thread t;
        protected override void LoadContent()
        {
            screen_transform = Matrix.CreateScale(GraphicsDevice.Viewport.Width / SpaceBattleGame.DEFAULT_SCR_WIDTH,
                GraphicsDevice.Viewport.Height / SpaceBattleGame.DEFAULT_SCR_HEIGHT, 1.0f);
            double_tap = Content.Load<Texture2D>("Menu/double_tap");
            single_tap = Content.Load<Texture2D>("Menu/single_tap");
            long_tap = Content.Load<Texture2D>("Menu/long_tap");
            team_logo = Content.Load<Texture2D>("Menu/vst_logo");
            game_title = Content.Load<Texture2D>("Menu/g_title_png_3");
            font_32 = Content.Load<SpriteFont>("Fonts/OCRAExtended30");
            font_16 = Content.Load<SpriteFont>("Fonts/OCRAExtended16");
            font_18 = Content.Load<SpriteFont>("Fonts/OCRAExtended18");
            click = Content.Load<SoundEffect>("Audio/button").CreateInstance();
            error = Content.Load<SoundEffect>("Audio/error").CreateInstance();

            tutorial1 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial1");
            tutorial2 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial2");
            tutorial3 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial3");
            tutorial4 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial4");
            tutorial5 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial5");
            tutorial6 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial6");
            tutorial7 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial7");
            tutorial8 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial8");
            tutorial9 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial9");

            audio_on = Game.Content.Load<Texture2D>(@"UI\audio_on");
            audio_off = Game.Content.Load<Texture2D>(@"UI\audio_off");
            audio_bar = Game.Content.Load<Texture2D>(@"UI\audio_bar");

            confirm = Game.Content.Load<Texture2D>("UI/confirm");
            not_conferm = Game.Content.Load<Texture2D>("UI/not_confirm");

            confirm_not_confirm_rectangle = new Rectangle(795 - confirm.Width , 0, confirm.Width, confirm.Height);

            top_left = new Rectangle(90, 75, 0, 0);
            middle_left = new Rectangle(90, 235, 0, 0);

            mini_top_left = new Rectangle(90, 75, 0, 0);
            mini_middle_left = new Rectangle(90, 175, 0, 0);
            mini_bottom_left = new Rectangle(90, 275, 0, 0);

            mini_top_middle = new Rectangle(400, 125, 0, 0);
            mini_bottom_middle = new Rectangle(400, 225, 0, 0);

            click.Volume = AudioManager.Volume; click.Pan = 0.0f; click.Pitch = 0.0f;
            error.Volume = AudioManager.Volume; error.Pan = 0.0f; error.Pitch = 0.0f;

            box = new Box(new Texture2D[] { Game.Content.Load<Texture2D>("UI/pause_menu_border"),
            Game.Content.Load<Texture2D>("UI/WhitePixel"), Game.Content.Load<Texture2D>("UI/pause_menu_border") },
                new Vector2(445, 70), new Vector2(320, 380), Color.Black, 0.8f);


            
            if (resume_option)
            {
                buttons = CreateRightAlignedButtons(new string[] { Strings.MainMenu_Resume , Strings.MainMenu_Play , Strings.MainMenu_HighScores, Strings.MainMenu_Controls, 
                Strings.MainMenu_Credits, Strings.MainMenu_Exit }, 15, 70, 10, 30 + game_title.Width);
            }
            else
            {
                buttons = CreateRightAlignedButtons(new string[] {  Strings.MainMenu_Play , Strings.MainMenu_HighScores, Strings.MainMenu_Controls, 
                Strings.MainMenu_Credits, Strings.MainMenu_Exit }, 30, 75, 10, 30 + game_title.Width);
            }

            audio = new Button
            {
                CollisionBox = new Rectangle(0, 0, audio_on.Width, audio_on.Height),
                Label = Strings.AudioOn,
                Position = new Vector2(0, 0)
            };

            audio_rect_bar = new Rectangle(0, 0, audio_bar.Width, audio_bar.Height);

            mode_buttons = CreateRightAlignedButtons(new string[] { Strings.Mode3, /*Strings.Mode2,*/ Strings.Mode1, Strings.Back },
                25, 220, 10, 30 + game_title.Width);

            tutorial_buttons = CreateRightAlignedButtons(new string[] { Strings.Prev, Strings.ExitTutorial, Strings.Next }, 250, 100, 430, 10, 0);
            back = CreateButtons(new string[] { Strings.Back }, 0, 420, 10)[0];
            resume = CreateRightAlignedButtons(new string[] { Strings.ContinueCamp }, 0, 210, 10, 30 + game_title.Width)[0];
            new_campaign = CreateRightAlignedButtons(new string[] { Strings.NewCampaing }, 0, 300, 10, 30 + game_title.Width)[0];
            co_back = CreateRightAlignedButtons(new string[] { Strings.Back }, 0, 390, 10, 30 + game_title.Width)[0];

            t = new Thread(() =>
            {
                // Ship Selection
                Content.Load<Texture2D>("Menu/title_bg");
                Content.Load<Texture2D>("Menu/stat_bg");
                Content.Load<Texture2D>("Menu/buy_box");
                Content.Load<SpriteFont>("Fonts/OCRAExtended32");
                Content.Load<SpriteFont>("Fonts/OCRAExtended18");
                Content.Load<SpriteFont>("Fonts/Orator14");
                Content.Load<SpriteFont>("Fonts/VerySmall");
                Content.Load<Texture2D>("UI/WhitePixel");
                Content.Load<SoundEffect>("Audio/error");

                // Various Model
                var c = ConfigurationService.GetDefaulConfiguration();

                for (int i = 0; i < c.Ships.Count; i++)
                    Content.Load<Model>(c.Ships[i].ModelPath);
            });

            t.Start();

            var sc = Leaderboard.GetScores();

            for (int k = 0; k < sc.Count; k++)
                scores.Add(sc[k]);

            scores = scores.OrderByDescending(s => s.Score).ToList();
            click.Volume = audiostate ? 1 : 0;
            base.LoadContent();
        }

        protected override void Dispose(bool disposing)
        {
            Services.RemoveService(typeof(IInputManager));

            base.Dispose(disposing);
        }

        public override void Update(GameTime gameTime)
        {
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            wait_time_to_tap -= dt;

            if (input_manager.IsBackButtonPressed() && menu_state == MenuState.MainMenu)
                Game.Exit();

            if (increaseAlpha)
            {
                if (!box.InTransition)
                    alpha = MathHelper.Clamp(alpha + dt * 2.0f, 0, 1);
            }
            else
            {
                alpha = MathHelper.Clamp(alpha - dt * 2.0f, 0, 1);
                if (alpha == 0.0f)
                {
                    increaseAlpha = true;
                    menu_state = next_state;
                }
            }

            if (increaseAlpha) box.Update(dt * 2.0f);

            base.Update(gameTime);
        }

        #region Update Helper

        void OnMainMenuButtonPressed(int tapped_button)
        {
            var tmp = Leaderboard.GetWakaModeStats();
            if (resume_option)
            {
                switch (tapped_button)
                {

                    case 0:
                        if (!Game.IsTrial())
                        {
                            if (!(click.State == SoundState.Playing)) click.Play();
                            ConfigurationService.ChangeConfiguration(Game, ConfigurationService.GetDefaulConfiguration());
                            StateManager.SelectSurvivalLevel(Game, t, true, audiostate ? 1 : 0);
                        }
                        else
                            error.Play();
                        break;
                    case 1:
                        if (!Game.IsTrial())
                        {
                            if (!(click.State == SoundState.Playing)) click.Play();
                            if (!show_tutorial)
                            {

                                ConfigurationService.ChangeConfiguration(Game, ConfigurationService.GetDefaulConfiguration());
                                StateManager.SelectSurvivalLevel(Game, t, false, audiostate ? 1 : 0);
                            }
                            else
                            {
                                game_tutorial = true;
                                tutorial_buttons[1].Label = Strings.Skip;
                                box.StartTransition(new Vector2(45, 50), new Vector2(710, 360));
                                StartTransition(MenuState.Controls);
                            }
                        }
                        else
                            error.Play();
                        break;

                    case 2:
                        if (!(click.State == SoundState.Playing)) click.Play();
                        box.StartTransition(new Vector2(195, 25), new Vector2(410, 380));
                        StartTransition(MenuState.HighScores);
                        break;
                    case 3:
                        if (!(click.State == SoundState.Playing)) click.Play();
                        box.StartTransition(new Vector2(45, 60), new Vector2(710, 350));
                        StartTransition(MenuState.Controls);
                        break;
                    case 4:
                        if (!(click.State == SoundState.Playing)) click.Play();
                        box.StartTransition(new Vector2(120, 70), new Vector2(560, 345));
                        StartTransition(MenuState.Credits);
                        break;
                    case 5:
                        if (!(click.State == SoundState.Playing)) click.Play();
                        var x = Leaderboard.GetWakaModeStats();
                        Game.Exit();
                        break;
                    default:
                        break;
                }
            }
            else
            {
                switch (tapped_button)
                {

                    case 0:
                        if (!Game.IsTrial())
                        {
                            if (!(click.State == SoundState.Playing)) click.Play();
                            if (!show_tutorial)
                            {

                                ConfigurationService.ChangeConfiguration(Game, ConfigurationService.GetDefaulConfiguration());
                                StateManager.SelectSurvivalLevel(Game, t, false, audiostate ? 1 : 0);
                            }
                            else
                            {
                                game_tutorial = true;
                                tutorial_buttons[1].Label = Strings.Skip;
                                box.StartTransition(new Vector2(45, 60), new Vector2(710, 350));
                                StartTransition(MenuState.Controls);
                            }
                        }
                        else
                            error.Play();
                        break;

                    case 1:
                        if (!(click.State == SoundState.Playing)) click.Play();
                        box.StartTransition(new Vector2(195, 25), new Vector2(410, 380));
                        StartTransition(MenuState.HighScores);
                        break;
                    case 2:
                        if (!(click.State == SoundState.Playing)) click.Play();
                        box.StartTransition(new Vector2(45, 60), new Vector2(710, 350));
                        StartTransition(MenuState.Controls);
                        break;
                    case 3:
                        if (!(click.State == SoundState.Playing)) click.Play();
                        box.StartTransition(new Vector2(120, 70), new Vector2(560, 345));
                        StartTransition(MenuState.Credits);
                        break;
                    case 4:
                        if (!(click.State == SoundState.Playing)) click.Play();
                        Leaderboard.SaveWakaModeStats(new WakaShipModeEntry()
                        {
                            TimeToEndGame = 0,
                            BossDestroyed = 0,
                            ShipsDestroyed = 0,
                            LevelScore = 0,
                            Level = 0,
                            AudioState = tmp.AudioState,
                            AsteroidHits = 0,
                            TimeToAddOrRemove = 0,
                            showAlwaysTutorial = tmp.showAlwaysTutorial
                        });
                        Game.Exit();
                        break;
                    default:
                        break;
                }
            }
        }

        private void MainMenuUpdate(int tap_x, int tap_y)
        {
            var tapped_button = -1;

            for (int i = 0; i < buttons.Count(); i++)
                if (buttons[i].CollisionBox.Contains(tap_x, tap_y))
                {
                    tapped_button = i;
                    break;
                }

            OnMainMenuButtonPressed(tapped_button);
        }

        void OnModeSelectionButtonPressed(int tapped_button)
        {
            //if (input_manager.IsBackButtonPressed()) tapped_button = 2;

            //switch (tapped_button)
            //{
            //  case 0:
            //    if (!Game.IsTrial())
            //    {
            //      if (!(click.State == SoundState.Playing)) click.Play();
            //      if (!campaign_completed)
            //        box.StartTransition(new Vector2(450, 170), new Vector2(300, 290));
            //      else
            //        box.StartTransition(new Vector2(450, 270), new Vector2(300, 190));
            //      ConfigurationService.ChangeConfiguration(Game, ConfigurationService.GetDefaulConfiguration());
            //    }
            //    else
            //      error.Play();
            //    break;
            //  //case 1:
            //  //    click.Play(AudioManager.Volume, 0.0f, 0.0f);
            //  //    ConfigurationService.ChangeConfiguration(Game, ConfigurationService.GetDefaulConfiguration());
            //  //    StateManager.SelectSkirmishOptionScreen(Game);
            //  //    break;
            //  //case 2:
            //  case 1:
            //    if (!(click.State == SoundState.Playing)) click.Play();
            //    ConfigurationService.ChangeConfiguration(Game, ConfigurationService.GetDefaulConfiguration());
            //    StateManager.SelectSurvivalLevel(Game, t);
            //    break;
            //  //case 3:
            //  case 2:
            //    if (!(click.State == SoundState.Playing)) click.Play();
            //    box.StartTransition(new Vector2(445, 70), new Vector2(320, 380));
            //    StartTransition(MenuState.MainMenu);
            //    break;
            //  default:
            //    break;
            //}
        }

        private void ModeSelectionUpdate(int tap_x, int tap_y)
        {
            var tapped_button = -1;

            for (int i = 0; i < mode_buttons.Count(); i++)
                if (mode_buttons[i].CollisionBox.Contains(tap_x, tap_y))
                {
                    tapped_button = i;
                    //if (!(click.State == SoundState.Playing)) click.Play();
                    break;
                }

            OnModeSelectionButtonPressed(tapped_button);
        }

        #endregion

        public override void Draw(GameTime gameTime)
        {
            sprite_batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screen_transform);

            //sprite_batch.Draw(bg, new Rectangle(0, 0, 480, 800), Color.White);

            sprite_batch.Draw(game_title, new Vector2(30, 100),
                Color.White);

            box.Draw(sprite_batch);

            sprite_batch.Draw(audio_bar, audio_rect_bar, Color.White);

            if (audiostate)
                sprite_batch.Draw(audio_on, audio.CollisionBox, Color.White);
            else
                sprite_batch.Draw(audio_off, audio.CollisionBox, Color.White);

            switch (menu_state)
            {
                case MenuState.MainMenu:
                    DrawMenu();
                    break;
                case MenuState.Credits:
                    DrawCredits();
                    break;
                case MenuState.Controls:
                    DrawControls();
                    break;
                case MenuState.HighScores:
                    DrawHighScores();
                    break;
                case MenuState.ModeSelection:
                    DrawSelectMode();
                    break;
            }

            sprite_batch.End();

            base.Draw(gameTime);
        }

        #region Private Methods

        #region OnTap & CreateButtons

        Button back, resume, new_campaign;

        Button[] tutorial_buttons;

        SoundEffectInstance click;
        private void on_tap(Vector2 tap_position)
        {
            if (wait_time_to_tap <= 0.0f)
            {
                int tap_x = (int)(tap_position.X / screen_transform.M11), tap_y = (int)(tap_position.Y / screen_transform.M22);

                if (audio.CollisionBox.Contains(tap_x, tap_y))
                {
                    audiostate = !audiostate;
                    click.Volume = audiostate ? 1 : 0;

                    var tmp = Leaderboard.GetWakaModeStats();

                    Leaderboard.SaveWakaModeStats(new WakaShipModeEntry(
                                                    tmp.showAlwaysTutorial,
                                                    audiostate,
                                                    tmp.AsteroidHits,
                                                    tmp.TimeToAddOrRemove,
                                                    tmp.TimeToEndGame,
                                                    tmp.ShipsDestroyed,
                                                    tmp.BossDestroyed,
                                                    tmp.LevelScore,
                                                    tmp.Level
                                                    ));

                    if (!(click.State == SoundState.Playing)) click.Play();
                }

                switch (menu_state)
                {
                    case MenuState.MainMenu:
                        MainMenuUpdate(tap_x, tap_y);
                        break;
                    case MenuState.ModeSelection:
                        ModeSelectionUpdate(tap_x, tap_y);
                        break;
                    default:
                        var x = (int)tap_position.X;
                        var y = (int)tap_position.Y;
                        if (confirm_not_confirm_rectangle.Contains(x, y))
                        {
                            if (!(click.State == SoundState.Playing)) click.Play();
                            var tmp = Leaderboard.GetWakaModeStats();
                            Leaderboard.SaveWakaModeStats(new WakaShipModeEntry(
                                                                !tmp.showAlwaysTutorial,
                                                                tmp.AudioState,
                                                                tmp.AsteroidHits,
                                                                tmp.TimeToAddOrRemove,
                                                                tmp.TimeToEndGame,
                                                                tmp.ShipsDestroyed,
                                                                tmp.BossDestroyed,
                                                                tmp.LevelScore,
                                                                tmp.Level
                                                                ));
                            show_tutorial = !show_tutorial;
                        }
                        else
                        {
                            for (int i = 0; i < tutorial_buttons.Count(); i++)
                            {
                                if (tutorial_buttons[i].CollisionBox.Contains(x, y))
                                {
                                    if (i == 0 && (tutorial_page > 0))
                                    {
                                        if (!(click.State == SoundState.Playing)) click.Play();
                                        tutorial_page--;
                                    }
                                    else if (i == 1)
                                    {
                                        if (!(click.State == SoundState.Playing)) click.Play();
                                        if (!game_tutorial)
                                        {
                                            box.StartTransition(new Vector2(445, 70), new Vector2(320, 380));
                                            StartTransition(MenuState.MainMenu);
                                        }
                                        else
                                        {
                                            ConfigurationService.ChangeConfiguration(Game, ConfigurationService.GetDefaulConfiguration());
                                            StateManager.SelectSurvivalLevel(Game, t, false, audiostate ? 1 : 0);
                                        }
                                        tutorial_page = 0;
                                    }
                                    else if (i == 2 && (tutorial_page < 2))
                                    {
                                        if (!(click.State == SoundState.Playing)) click.Play();
                                        tutorial_page++;
                                    }
                                }
                            }
                            if (back.CollisionBox.Contains(tap_x, tap_y) && !(menu_state == MenuState.Controls) || input_manager.IsBackButtonPressed())
                            {
                                if (!(click.State == SoundState.Playing)) click.Play();
                                box.StartTransition(new Vector2(445, 70), new Vector2(320, 380));
                                StartTransition(MenuState.MainMenu);
                            }                            
                        }
                        break;
                }
            }
        }

        private Button[] CreateButtons(string[] labels, int offset, int starting_y, int padding)
        {
            int w = GraphicsDevice.Viewport.Width;
            int h = GraphicsDevice.Viewport.Height;
            Button[] bs = new Button[labels.Count()];

            for (int i = 0; i < bs.Count(); i++)
            {
                Vector2 label_measure = font_32.MeasureString(labels[i]);
                int x = (int)(w / 2 - label_measure.X / 2.0f);
                int y = starting_y + i * (offset + (int)label_measure.Y);
                bs[i] = new Button()
                {
                    CollisionBox = new Rectangle(x - padding, y - padding, (int)label_measure.X + padding * 2, (int)label_measure.Y + padding * 2),
                    Label = labels[i],
                    Position = new Vector2(x, y)
                };
            }

            return bs;
        }

        private enum ButtonAligment
        {
            Left,
            Right
        }

        private Button[] CreateRightAlignedButtons(string[] labels, int offset, int starting_y, int padding, int align_val)
        {
            int w = GraphicsDevice.Viewport.Width;
            int h = GraphicsDevice.Viewport.Height;
            Button[] bs = new Button[labels.Count()];

            for (int i = 0; i < bs.Count(); i++)
            {
                Vector2 label_measure = font_32.MeasureString(labels[i]);
                int x = (int)((w + align_val) / 2.0f - label_measure.X / 2.0f);
                int y = starting_y + i * (offset + (int)label_measure.Y);
                bs[i] = new Button()
                {
                    CollisionBox = new Rectangle(x - padding, y - padding, (int)label_measure.X + padding * 2, (int)label_measure.Y + padding * 2),
                    Label = labels[i],
                    Position = new Vector2(x, y)
                };
            }

            return bs;
        }

        private Button[] CreateRightAlignedButtons(string[] labels, int offset, int starting_x, int starting_y, int padding, int align_val)
        {
            int w = GraphicsDevice.Viewport.Width;
            int h = GraphicsDevice.Viewport.Height;
            Button[] bs = new Button[labels.Count()];

            for (int i = 0; i < bs.Count(); i++)
            {
                Vector2 label_measure = font_32.MeasureString(labels[i]);
                var x = starting_x + i * offset;
                //int x = (int)((w + align_val) / 2.0f - label_measure.X / 2.0f);
                int y = starting_y;
                bs[i] = new Button()
                {
                    CollisionBox = new Rectangle(x, y, (int)label_measure.X, (int)label_measure.Y),
                    Label = labels[i],
                    Position = new Vector2(x, y)
                };
            }

            return bs;
        }


        #endregion

        private void StartTransition(MenuState next)
        {
            next_state = next;
            increaseAlpha = false;
        }

        #region Draw Helper

        #region HighScores

        List<LeaderboardEntry> scores = new List<LeaderboardEntry>();
        private void DrawHighScores()
        {
            var title = Strings.MainMenu_HighScores;
            sprite_batch.DrawString(font_32, title, new Vector2(GraphicsDevice.Viewport.Width / 2.0f - font_32.MeasureString(title).X / 2.0f,
                40) + Vector2.One * 2.5f, Color.Black * alpha);
            sprite_batch.DrawString(font_32, title, new Vector2(GraphicsDevice.Viewport.Width / 2.0f - font_32.MeasureString(title).X / 2.0f,
                40), Color.White * alpha);

            int i = 0;

            for (int y = 0; y < scores.Count; y++)
            {
                var s = scores[y];
                sprite_batch.DrawString(font_16, s.Name, new Vector2(250, 130 + 40 * i), Color.White * alpha);
                sprite_batch.DrawString(font_16, s.Score.ToString(), new Vector2(370, 130 + 40 * i), Color.White * alpha);
                sprite_batch.DrawString(font_16, s.Date, new Vector2(450, 130 + 40 * i), Color.White * alpha);
                i++;
            }

            if (i == 0)
            {
                sprite_batch.DrawString(font_16, "--", new Vector2(Game.GraphicsDevice.Viewport.Width / 2 - 10.0f, 150), Color.White * alpha);
            }

            sprite_batch.DrawString(font_32, back.Label, back.Position + Vector2.One * 2.5f, Color.Black * alpha);
            sprite_batch.DrawString(font_32, back.Label, back.Position, Color.White * alpha);
        }

        #endregion

        #region Credits
        Texture2D single_tap, double_tap, long_tap, team_logo;
        private void DrawCredits()
        {
            sprite_batch.Draw(team_logo, new Rectangle(155, (int)(GraphicsDevice.Viewport.Height / 2.0f - 39), 200, 78),
                Color.White * alpha);

            //sprite_batch.DrawString(font_16, "Lead Developer:", new Vector2(120, 380), Color.White * alpha);
            sprite_batch.DrawString(font_18, "Giuseppe Maggiore", new Vector2(390, 80), Color.White * alpha);
            sprite_batch.DrawString(font_18, "Andrea Gasparetto", new Vector2(390, 110), Color.White * alpha);
            sprite_batch.DrawString(font_18, "Mohamed Abbadi", new Vector2(390, 140), Color.White * alpha);
            sprite_batch.DrawString(font_18, "Mario Soprano", new Vector2(390, 170), Color.White * alpha);
            sprite_batch.DrawString(font_18, "Enrico Steffinlongo", new Vector2(390, 200), Color.White * alpha);
            sprite_batch.DrawString(font_16, "Special Thanks:", new Vector2(350, 230), Color.White * alpha);
            sprite_batch.DrawString(font_18, "Luca Cosmo", new Vector2(390, 260), Color.White * alpha);
            sprite_batch.DrawString(font_18, "Davide Luzzu", new Vector2(390, 290), Color.White * alpha);
            sprite_batch.DrawString(font_18, "Francesco Usai", new Vector2(390, 320), Color.White * alpha);
            sprite_batch.DrawString(font_18, "Edoardo Micheli", new Vector2(390, 350), Color.White * alpha);

            sprite_batch.DrawString(font_32, back.Label, back.Position + Vector2.One * 2.5f, Color.Black * alpha);
            sprite_batch.DrawString(font_32, back.Label, back.Position, Color.White * alpha);
        }

        #endregion

        #region Controls

        private void DrawControls()
        {
            if (show_tutorial)
                sprite_batch.Draw(confirm, confirm_not_confirm_rectangle, Color.White * alpha);
            else
                sprite_batch.Draw(not_conferm, confirm_not_confirm_rectangle, Color.White * alpha);


            sprite_batch.DrawString(font_16,Strings.Tutorial_Option.Replace("\\n", "\n"),
                       new Vector2(confirm_not_confirm_rectangle.X - 270, confirm_not_confirm_rectangle.Y + 10), Color.DarkGray * alpha);
            sprite_batch.DrawString(font_16,Strings.Tutorial_Option.Replace("\\n", "\n"),
                new Vector2(confirm_not_confirm_rectangle.X - 271, confirm_not_confirm_rectangle.Y + 10), Color.White * alpha);            

            switch (tutorial_page)
            {
                case 0:

                    sprite_batch.DrawString(font_16,Strings.TitleTutorial1 +" \n"+ Strings.Tutorial1.Replace("\\n", "\n"),
                        new Vector2(421, top_left.Y), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.TitleTutorial1 + " \n" + Strings.Tutorial1.Replace("\\n", "\n"),
                        new Vector2(420, top_left.Y), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial2.Replace("\\n", "\n"),
                        new Vector2(421, middle_left.Y), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial2.Replace("\\n", "\n"),
                        new Vector2(420, middle_left.Y), Color.White * alpha);

                    top_left.Width = tutorial2.Width;
                    top_left.Height = tutorial2.Height;
                    sprite_batch.Draw(tutorial2, top_left, Color.White * alpha);

                    middle_left.Width = tutorial1.Width;
                    middle_left.Height = tutorial1.Height;
                    sprite_batch.Draw(tutorial1, middle_left, Color.White * alpha);

                    for (int i = 1; i < tutorial_buttons.Count(); i++)
                    {
                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position + Vector2.One * 2.5f, Color.Black * alpha);
                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position, Color.White * alpha);
                    }

                    break;
                case 2:


                    sprite_batch.DrawString(font_16, Strings.Tutorial3.Replace("\\n", "\n"),
                        new Vector2(mini_top_left.X + tutorial3.Width + 3, mini_top_left.Y), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial3.Replace("\\n", "\n"),
                        new Vector2(mini_top_left.X + tutorial3.Width + 4, mini_top_left.Y), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial4.Replace("\\n", "\n"),
                        new Vector2(mini_middle_left.X + tutorial4.Width + 3, mini_middle_left.Y), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial4.Replace("\\n", "\n"),
                        new Vector2(mini_middle_left.X + tutorial4.Width + 4, mini_middle_left.Y), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial5.Replace("\\n", "\n"),
                        new Vector2(mini_bottom_left.X + tutorial5.Width + 3, mini_bottom_left.Y ), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial5.Replace("\\n", "\n"),
                        new Vector2(mini_bottom_left.X + tutorial5.Width + 4, mini_bottom_left.Y ), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial6.Replace("\\n", "\n"),
                        new Vector2(mini_top_middle.X + tutorial6.Width + 3, mini_top_middle.Y ), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial6.Replace("\\n", "\n"),
                        new Vector2(mini_top_middle.X + tutorial6.Width + 4, mini_top_middle.Y ), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial7.Replace("\\n", "\n"),
                        new Vector2(mini_bottom_middle.X + tutorial7.Width + 3, mini_bottom_middle.Y ), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial7.Replace("\\n", "\n"),
                        new Vector2(mini_bottom_middle.X + tutorial7.Width + 4, mini_bottom_middle.Y ), Color.White * alpha);

                    mini_top_left.Width = tutorial3.Width;
                    mini_top_left.Height = tutorial3.Height;
                    sprite_batch.Draw(tutorial3, mini_top_left, Color.White * alpha);

                    mini_middle_left.Width = tutorial4.Width;
                    mini_middle_left.Height = tutorial4.Height;
                    sprite_batch.Draw(tutorial4, mini_middle_left, Color.White * alpha);

                    mini_bottom_left.Width = tutorial5.Width;
                    mini_bottom_left.Height = tutorial5.Height;
                    sprite_batch.Draw(tutorial5, mini_bottom_left, Color.White * alpha);

                    mini_top_middle.Width = tutorial6.Width;
                    mini_top_middle.Height = tutorial6.Height;
                    sprite_batch.Draw(tutorial6, mini_top_middle, Color.White * alpha);

                    mini_bottom_middle.Width = tutorial7.Width;
                    mini_bottom_middle.Height = tutorial7.Height;
                    sprite_batch.Draw(tutorial7, mini_bottom_middle, Color.White * alpha);

                    var tmp = tutorial_buttons[1].Label;
                    tutorial_buttons[1].Label = Strings.ExitTutorial;

                    for (int i = 0; i < tutorial_buttons.Count() - 1; i++)
                    {
                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position + Vector2.One * 2.5f, Color.Black * alpha);
                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position, Color.White * alpha);
                    }
                    tutorial_buttons[1].Label = tmp;

                    break;
                case 1:

                    sprite_batch.DrawString(font_16,Strings.TitleTutorial8 +" \n"+ Strings.Tutorial8.Replace("\\n", "\n"),
                        new Vector2(421, top_left.Y), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.TitleTutorial8 + " \n" + Strings.Tutorial8.Replace("\\n", "\n"),
                        new Vector2(420, top_left.Y), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial9.Replace("\\n", "\n"),
                        new Vector2(421, middle_left.Y), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial9.Replace("\\n", "\n"),
                        new Vector2(420, middle_left.Y), Color.White * alpha);

                    top_left.Width = tutorial8.Width;
                    top_left.Height = tutorial8.Height;
                    sprite_batch.Draw(tutorial8, top_left, Color.White * alpha);

                    middle_left.Width = tutorial9.Width;
                    middle_left.Height = tutorial9.Height;
                    sprite_batch.Draw(tutorial9, middle_left, Color.White * alpha);
                    for (int i = 0; i < tutorial_buttons.Count(); i++)
                    {
                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position + Vector2.One * 2.5f, Color.Black * alpha);
                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position, Color.White * alpha);
                    }

                    break;

            }






        }

        #endregion

        #region Menu

        Button[] buttons;
        Button audio;
        SpriteFont font_32, font_16;
        Texture2D game_title;

        void DrawMenu()
        {

            for (int i = 0; i < buttons.Count(); i++)
            {
                sprite_batch.DrawString(font_32, buttons[i].Label, buttons[i].Position + Vector2.One * 2.5f, Color.Black * alpha);
                sprite_batch.DrawString(font_32, buttons[i].Label, buttons[i].Position, Color.White * alpha);
            }

        }

        #endregion

        #region SelectGameMode

        Button[] mode_buttons;
        private SoundEffectInstance error;
        private SpriteFont font_18;
        private Button co_back;

        void DrawSelectMode()
        {
            var a = 1.0f;
            for (int i = 0; i < mode_buttons.Count(); i++)
            {
                if (i == 0 && Game.IsTrial())
                    a = 0.5f;
                sprite_batch.DrawString(font_32, mode_buttons[i].Label, mode_buttons[i].Position + Vector2.One * 2.5f, Color.Black * (Math.Min(alpha, a)));
                sprite_batch.DrawString(font_32, mode_buttons[i].Label, mode_buttons[i].Position, Color.White * (Math.Min(alpha, a)));
            }

        }

        void DrawCampaignOption()
        {
            if (!campaign_completed)
            {
                sprite_batch.DrawString(font_32, resume.Label, resume.Position + Vector2.One * 2.5f, Color.Black * alpha);
                sprite_batch.DrawString(font_32, resume.Label, resume.Position, Color.White * alpha);
            }

            sprite_batch.DrawString(font_32, new_campaign.Label, new_campaign.Position + Vector2.One * 2.5f, Color.Black * alpha);
            sprite_batch.DrawString(font_32, new_campaign.Label, new_campaign.Position, Color.White * alpha);

            sprite_batch.DrawString(font_32, co_back.Label, co_back.Position + Vector2.One * 2.5f, Color.Black * alpha);
            sprite_batch.DrawString(font_32, co_back.Label, co_back.Position, Color.White * alpha);
        }

        #endregion

        #endregion

        #endregion

        ContentManager Content { get { return Game.Content; } }
        GameServiceContainer Services { get { return Game.Services; } }
        GameComponentCollection Components { get { return Game.Components; } }
    }
}
